﻿using Microsoft.EntityFrameworkCore;

namespace LuckyFish.Blog.Models;

public class BlogContext : DbContext
{
    public BlogContext(DbContextOptions<BlogContext> options)
        : base(options) { }

    public DbSet<ArticleModel> Articles { get; set; }
    public DbSet<AdminModel> Admins { get; set; }
    public DbSet<ArticleList> ArticleLists { get; set; }
    public DbSet<LabelList> LabelLists { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ArticleList>()
            .HasMany(e => e.Articles)
            .WithOne(e => e.List)
            .HasForeignKey(e => e.ListId)
            .IsRequired(false);
        
        modelBuilder.Entity<LabelList>()
            .HasMany(e => e.Articles)
            .WithMany(e => e.Labels);
    }
}